package com.bos.controller;

import com.bos.data.model.BosProjectModel;
import com.bos.data.model.BosUserModel;
import com.bos.service.AccessTokenService;
import com.bos.service.Contacts_UserService;
import com.bos.service.LogsService;
import com.bos.service.UserService;
import com.bos.util.QiWeiXinParamesUtil;
import com.bos.util.WeiXinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/*
 *
 * 这是一个操作用户的控制器（进行后台将数据返回给jsp页面）
 *
 * */
@Controller
public class UserController {
    public static int power;
    public static String name;
    @Autowired
    @Qualifier("userServiceImpl")
    UserService userService;


    @Autowired
    @Qualifier("accessTokenServiceImpl")
    AccessTokenService accService;


    @Autowired
    private LogsService logsService;
    //首页
  /*  @RequestMapping("/loginForm")
    public String loginForm(
            Model model
    ) {
        BosUserModel user = new BosUserModel();
        model.addAttribute("command", user);
        return "login";
    }
*/
    //用户注册
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@ModelAttribute BosUserModel user, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = user.getName();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        byte[] bytes = username.getBytes("ISO-8859-1");
        username = new String(bytes, "UTF-8");
        boolean isSuc = userService.register(user);
        if (isSuc == true) {
            model.addAttribute("command", user);
            return "login";
        }
        return "register";
    }

    //用户登录
    List<BosProjectModel> pro;

    @RequestMapping("/login")
    public String login(@ModelAttribute BosUserModel user, HttpSession session, Model model, RedirectAttributes attr) {
        BosUserModel u = userService.login(user);
        if (u != null) {
            model.addAttribute("user", u);
            attr.addFlashAttribute("user", u);
            name = user.getName();
            //根据用户名去匹配数据
            BosUserModel use = userService.findUserByName(u.getName());
            power = use.getPower();
            session.setAttribute("user", u);
            List<BosUserModel> users = userService.findUsers();
            session.setAttribute("allUser",users);
            System.out.println("登陆的用户为：" + u.getName());
            return "redirect:pcmt";
		/*	//根据用户名去匹配数据
			User use=userService.findUserByName(u.getName());
			//如果为系统管理员
			if(use.getPower()==0){
				return "redirect:showAll";
			}

			//如果登陆的用户为管理员
			if(use.getPower()==1){
				//根据名字查询出Project中的数据
				power=1;
				pro=userService.findProjectByName(u.getName());
				return "redirect:showAllByName";
			}
			//如果登陆的用户为普通用户
			if(use.getPower()==2){
				//根据名字查询出Project中的数据
				power=2;
				pro=userService.findProjectByName(u.getName());
				return "redirect:showAllByName";
			}*/
        }
        user.setPassword("");
        model.addAttribute("command", user);
       // model.addAttribute("msg", "用户名或密码错误，请重新输入.." );
        return "login";
    }

    //这个是用来不同的权限显示不同的数据的方法
    @RequestMapping(value = "/showAllByName")
    public String findAll(Model model, HttpSession session) {
        session.setAttribute("projects", pro);
        return "bosIndex";
    }

    //1.调用创建成员的接口
    @RequestMapping(value = "/createUser")
    public String createUser(@RequestParam("name") String name, @RequestParam("zhanghao") String zhanghao, @RequestParam("department") String department,
                             @RequestParam("gender") String gender, @RequestParam("mobile") String mobile, @RequestParam("email") String email,
                             @RequestParam("position") String position, Model model) {
        BosUserModel user = new BosUserModel();
        user.setName(name);
        user.setZhanghao(zhanghao);
        user.setDepartment(department);
        user.setGender(gender);
        user.setMobile(mobile);
        user.setEmail(email);
        user.setPosition(position);
        Contacts_UserService userService = new Contacts_UserService();
        String accessToken = WeiXinUtil.getAccessToken(accService, "contacts", QiWeiXinParamesUtil.corpId, QiWeiXinParamesUtil.contactsSecret);
        userService.createUser(accessToken, user);

        return "";


    }

    //查询出bos_user表中的数据
    @RequestMapping(value = "/findUser")
    public String findUsers(Model model) {
        List<BosUserModel> users = userService.findUsers();
        model.addAttribute("users", users);
        return "jsp/addPeople";

    }
    //进行登陆用户的注销操作
    @RequestMapping(value="/finishOff")
    public String finishOff(HttpServletRequest request){
        //注销，删除session
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();

        return "redirect:login";
    }

    @RequestMapping(value = "/updPassword")
    public String updPassword(HttpServletRequest request,HttpSession session,Model model){
            String newPass=request.getParameter("newPass");
            BosUserModel user= (BosUserModel) session.getAttribute("user");
            userService.updatePass(user.getId(),newPass);
            model.addAttribute("msg","true");
            logsService.insertLogs(session,"修改用户登录密码");
            return "jsp/changePassword";

    }
}
